package com.example.service;

import com.example.mapper.DoctorMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Doctor;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.utils.JwtUtils;
import com.example.utils.MD5Utils;
import com.example.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.print.Doc;
import java.sql.Array;
import java.util.*;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoctorMapper doctorMapper;



    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public Result login(String username, String password) {

        /**
         * 非空校验
         */
        if(!StringUtils.hasText(username)){
            return new Result(100,"用户名不能为空",null);
        }

        if (!StringUtils.hasText(password)){
            return new Result(100,"密码不能为空",null);
        }
        /**
         * 查询
         */
        //MD5加密
        //
        String md5Password = MD5Utils.stringToMD5(password);

        System.err.println(md5Password);

        User user = userMapper.getUserByNameAndPwd(username,md5Password);

        if (user==null){
            return new Result(100,"账号或者密码错误",null);
        }

        String token = JwtUtils.getJwtToken(user.getUname());

        RedisUtil.set("medical:login:token",token,60);

        Map<String,String>map=new HashMap<>();

        map.put("token",token);

        map.put("name",user.getUname());

        return new Result(200,"登录成功",map);
    }

    /**
     * 查询（有参）
     * 分页（无参/null）
     * @param realname
     * @return
     */
    public List<User> getUserList(String realname){

        return userMapper.getUserList(realname);

    }

    /**
     *添加用户
     * @param user
     * @return
     */
    public Result addUserInformation(User user){
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(user.getUname())){
            return new Result(100,"用户名不能为空",null);
        }
        if(!StringUtils.hasText(user.getPwd())){
            return new Result(100,"密码不能为空",null);
        }
        if(!StringUtils.hasText(user.getPhonenumber())){
            return new Result(100,"电话号码不能为空",null);
        }
        if(!StringUtils.hasText(user.getRealname())){
            return new Result(100,"真实姓名不能为空",null);
        }
        if(!StringUtils.hasText(user.getUtype())){
            return new Result(100,"用户类型不能为空",null);
        }
        /**
         * 是否存在
         */
        User u=userMapper.getUserByUname(user.getUname());
        if(u!=null){
            return new Result(100,"该用户名已存在",null);
        }
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());

       // System.err.println(user);
        //密码加密
        String md5Pwd=MD5Utils.stringToMD5(user.getPwd());
        user.setPwd(md5Pwd);

        //影响的函数
        int row = userMapper.insert(user);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"操作成功",null);
    }

    /**
     * 修改数据
     * @param user
     * @return
     */
    public Result edit(User user){
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(user.getUname())){
            return new Result(100,"用户名不能为空",null);
        }
        if(!StringUtils.hasText(user.getPwd())){
            return new Result(100,"密码不能为空",null);
        }
        if(!StringUtils.hasText(user.getPhonenumber())){
            return new Result(100,"电话号码不能为空",null);
        }
        if(!StringUtils.hasText(user.getRealname())){
            return new Result(100,"真实姓名不能为空",null);
        }
        if(!StringUtils.hasText(user.getUtype())){
            return new Result(100,"用户类型不能为空",null);
        }
        /**
         * 校验密码
         *          String md5pwd=MD5Utils.stringToMD5(user.getPwd());
         *
         *         User u=userMapper.getUserById(user.getId());
         *
         *         if(!md5pwd.equals(u.getPwd())){
         *             return new Result(100,"密码错误，无法修改",null);
         *         }
         */

        String md5pwd=MD5Utils.stringToMD5(user.getPwd());

        user.setPwd(md5pwd);

        user.setUpdatetime(new Date());

        int row=userMapper.update(user);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }

        return new Result(200,"修改成功",null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Result delUserById(int id) {
        /**
         * 查询是否存在
         */
        User u=userMapper.getUserById(id);
        if(u==null){
            return new Result(100,"用户不存在",null);
        }

        /**
         * 角色为医生同步修改doctor表
         */
        u=userMapper.getUserByAccount(id);

        if(u.getUtype().equals("ROLE_2")){

            Doctor dc=doctorMapper.GetDoctorByAccountId(id);

            int row1 =doctorMapper.delDoctorById(dc.getId());

            if(row1!=1) {
                return new Result(100, "清除相关数据失败", null);
            }
        }
        int row =userMapper.delUserById(id);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }

        return new Result(200,"删除成功",null);
    }

    public Result delBatch(String ids) {

        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =userMapper.delBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }
}
